/*
 * Created on 2005-7-6
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sohu.sip.auth.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.sohu.peak.util.Logger;
import com.sohu.peak.util.Pagination;
import com.sohu.sip.auth.business.bean.ActorBean;
import com.sohu.sip.auth.business.event.ActorAddEvent;
import com.sohu.sip.auth.business.event.ActorListEvent;
import com.sohu.sip.auth.business.event.ActorUpdateEvent;
import com.sohu.sip.auth.business.result.ActorAddResult;
import com.sohu.sip.auth.business.result.ActorListResult;
import com.sohu.sip.auth.business.result.ActorUpdateResult;
import com.sohu.sip.auth.po.Actor;

/**
 * @author luciali
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AuthActorAction extends BaseAction {
	private static Logger log = new Logger();
	private Actor actor;
	

	private List<Actor> actorList;
	private String name;
	private Long id;
	
    
	// logic method

	public Actor getActor() {
		return actor;
	}

	public void setActor(Actor actor) {
		this.actor = actor;
	}

	public List<Actor> getActorList() {
		return actorList;
	}

	public void setActorList(List<Actor> actorList) {
		this.actorList = actorList;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public String addActor() {
		return "success";
	}
	public String listActor() {
		
//		if (id != null) {
//			actor = this.getAuthFacade().getActor(id);
//			return "update";
//		}

		if (getPagination() == null) {
			Pagination pagination = new Pagination(1,20);
			this.setPagination(pagination);
		}

        this.getPagination().setPageCount(20);
		
		HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
		
		ActorListEvent event = new ActorListEvent(getName(),
				getStatus(), 0, 0
				);
		ActorListResult result = ActorBean.getInstance().list(event,getPagination());
		request.setAttribute("result", result);
		request.setAttribute("pagination", result.getPagination());
		if (result.getStatus() == 0)
			return "success";
		else
			return "failure";
	}
	
	public String insertActor() {
		try {
			ActorAddEvent event = new ActorAddEvent(actor.getName(),
					actor.getPriority(),
					actor.getStatus(),
					actor.getDescription());
			
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			
			ActorAddResult result = ActorBean.getInstance().add(event);
			request.setAttribute("result", result);

			if (result.getStatus() == 0) {
				return "success";
			} else {
				return "failure";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}

	public String modifyActor() {
		try {
			ActorUpdateEvent event = new ActorUpdateEvent(actor.getId());
			ActorUpdateResult result = ActorBean.getInstance().getDetail(event);
			
			HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
			request.setAttribute("result", result);

			if (result.getStatus() == 0)
				return "success";
			else
				return "failure";
		} catch (Exception e) {
			log.error(e.getMessage());
			return "failure";
		}
	}
	
	public String updateActor() {
		ActorUpdateEvent event = new ActorUpdateEvent(actor.getId(),
				actor.getName(),
				actor.getPriority(),
				actor.getStatus(),
				actor.getDescription());
		
		HttpServletRequest request=org.apache.struts2.ServletActionContext.getRequest();
		ActorUpdateResult result = ActorBean.getInstance().update(event);
		request.setAttribute("result", result);
		if (result.getStatus() == 0) return "success";
		else return "failure";
	}

	public String success() {
		return "success";
	}

	public String failure() {
		return "failure";
	}

}
